<!--
* description: 整车销售---订单收款
* author:xlc
* createdDate: 2020-11-23
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
    <edit
      v-if="editPopupsVisible"
      :key="editPopupsKey"
      :popups-visible="editPopupsVisible"
      :data-for-form="editRowData"
      :popups-state="editPopupsState"
      :title-name="titleName"
      @close="close"
      @save="save"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { orgApis } from '@/api/vesalesapi.js'
import OneTableTemplate from '@/components/templates/oneTable'
import Edit from './edit'
import Vue from 'vue'
export default {
  name: 'OrderCollection',
  components: {
    OneTableTemplate,
    Edit
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'supplierMessage-edit'], () => {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      titleName: '',
      editRowData: {},
      editPopupsState: '',
      editPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: orgApis.orderCollectionQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        }
        // {
        //   compKey: 'btnKey5',
        //   type: '',
        //   size: 'small',
        //   clickEvent: () => { this.sync() },
        //   text: '同步', // 新增
        //   name: 'add',
        //   position: 'left'
        // }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '订单编号：',
              codeField: 'saleOrderCode',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '客户姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '客户电话：',
              codeField: 'custTel',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // {
            //   compKey: 'compKey6',
            //   labelName: '付款人：',
            //   codeField: 'payer',
            //   component: () => import('@/components/org/commonInput'),
            //   type: 'inputText',
            //   isMust: true
            // },
            {
              compKey: 'compKey4',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // {
            //   compKey: 'compKey5',
            //   labelName: '收款时间：',
            //   codeField: 'gateringDateStartTime,gateringDateEndTime',
            //   component: () => import('@/components/org/datePicker/datePicker.vue'),
            //   dateType: 'daterange',
            //   isMul: true,
            //   isMust: false
            // },
            {
              compKey: 'compKey7',
              labelName: '交付门店：',
              codeField: 'deliveryDlrId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              oFields: 'deliveryDlrId,deliveryDlrShortName',
              customObj: {
                api: orgApis.ListOfDeliveryStore
              }
            },
            {
              compKey: 'compKey8',
              labelName: '交付专员：',
              codeField: 'empId',
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMust: false,
              parentFileds: 'value:deliveryDlrId',
              oFields: 'empId,empName',
              customObj: {
                listQuery: { deliveryDlrId: '' }, selectType: '1',
                api: orgApis.ListOfdeliveryPersonnel
              }
            }
            // {
            //   compKey: 'compKey9',
            //   labelName: '订单状态：',
            //   codeField: 'orderStatus',
            //   lookuptype: 'VE1003',
            //   component: () => import('@/components/org/LookupValue'),
            //   type: 'dropdownList',
            //   isMul: false,
            //   isMust: false
            // }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              width: 100,
              fixed: true,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '收款', // 编辑
                  align: 'center',
                  codeField: 'editControlBtn',
                  rowFileds: 'show:orderStatus--尾款已支付',
                  clickEvent: index => { this.edit(index) },
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'saleOrderCode',
              label: '订单编号', width: 120
            },
            // {
            //   prop: 'orderStatus',
            //   label: '订单状态', width: 120
            // },
            {
              prop: 'vin',
              label: 'VIN码', width: 160
            },
            {
              prop: 'custName',
              label: '客户姓名',
              width: 120
            },
            {
              prop: 'custTel',
              label: '客户电话',
              width: 180
            },
            {
              prop: 'dlrShortName',
              label: '交付门店', width: 120
            },
            {
              prop: 'empName',
              label: '交付专员', width: 120
            },
            {
              prop: 'signatoryName',
              label: '合同签署人', width: 120
            },
            {
              prop: 'receivedDepoist',
              label: '已收定金', width: 120
            },
            {
              prop: 'factReceivedAmount',
              label: '已收尾款金额', width: 120
            },
            {
              prop: 'receivedAmount',
              label: '已收总金额', width: 120
            },
            {
              prop: 'gatheringWeiPrice',
              label: '应收尾款金额', width: 120
            },
            {
              prop: 'gatheringPrice',
              label: '整车金额', width: 120
            },
            {
              prop: 'balanceDate',
              label: '收款完成时间', width: 180
            }
          ],
      formField: {
        saleOrderCode: '',
        custName: '',
        custTel: '',
        vin: '',
        // gateringDateStartTime: '',
        // gateringDateEndTime: '',
        // payer: '',
        deliveryDlrId: '',
        empId: '',
        orderStatus: '',
        pages: '',
        records: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      // 单表格模版页面组件回调事件
      callbackObj: {
        // // 手动勾选时
        // onCheckSelect: (selection, row) => {
        //   // 清除所有选中
        //   this.$refs.multipleTable.$refs.multipleTable.clearSelection()
        //   if (selection.length === 0) return
        //   // 将当前点击项选中
        //   this.$refs.multipleTable.$refs.multipleTable.toggleRowSelection(row, true)
        // },
        // // 当选择项发生变化时会触发该事件（多选时）
        // onCheckSelectChange: (selection) => {
        //   if (selection.length === 1) {
        //     this.editRowData = selection[0]
        //   }
        // }
      }
    }
  },
  mounted() {
    this.queryTable(1)
    // this.$refs.multipleTable.list = [{
    //   index: 1
    // }]
  },
  methods: {
    // 同步
    sync() {},
    // 删除
    deleteRow(index) {
      this.$refs.multipleTable.list.splice(index, 1)
    },
    // 保存
    save(res) {
      if (res.index) {
        this.$refs.multipleTable.list[res.index] = res
        this.close()
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
      } else {
        this.$refs.multipleTable.list.push(res)
        this.close()
        this.$message({
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
      }
    },
    // 导出
    exportExcel() {
    },
    addone() {
      this.add()
    },
    change(index) {
      this.editPopupsVisible = true
      this.editRowData = this.$refs.multipleTable.list[index]
    }
  }
}
</script>
